

#include <gfx/ibillboardmesh.h>
#include <gfx/renderenvironment.h>
#include <scene/impostereffectcamera.h>
#include <scene/node.h>
#include <utils/cameraprojectorsetup.h>

ceImposterEffectCamera::ceImposterEffectCamera()
  : _node(0)
  , _renderTarget(0)
  , _rerenderAngle (M_PI / 100.0)
  , _hasImage(false)
  , _billboardMesh(0)
{
}

void ceImposterEffectCamera::SetNode(ceNode *node)
{
  _node = node;
}



void ceImposterEffectCamera::SetRenderTarget(iRenderTarget *renderTarget)
{
  _renderTarget = renderTarget;
}

void ceImposterEffectCamera::SetBillboardMesh(iBillboardMesh *mesh)
{
  _billboardMesh = mesh;
}

iRenderTarget* ceImposterEffectCamera::GetRenderTarget()
{
  return _renderTarget;
}

const ceCamera& ceImposterEffectCamera::GetCamera() const
{
  return _camera;
}

const ceProjector& ceImposterEffectCamera::GetProjector() const
{
  return _projector;
}

ceNode* ceImposterEffectCamera::GetNode()
{
  return _node;
}

bool ceImposterEffectCamera::Update (ceRenderEnvironment& env, iDevice* device)
{
  const ceNode* n = _node;
  bool needRerender = false;
  ceVector3f d = n->GetBoundingBox().GetCenter() - env.CameraLocationGlobal;
  if (!_hasImage)
    {
      needRerender = true;
    }
  else
    {
      float a = d.Angle(_lastDirection);
      needRerender = a > _rerenderAngle;
    }
  if (needRerender)
    {
      ceCameraProjectorSetup setup;
      ceVector2f size;
      setup.SetupPerspective(env.CameraLocationGlobal, ceVector3f(0, 0, 1), n->GetBoundingBox(), _camera, _projector, size);
      _billboardMesh->SetSize (size);
      _hasImage = true;
      _lastDirection = d;
    }
  return needRerender;
}


